//http://www.pplware.com/feed/
//http://feedproxy.google.com/leopoldomoreira/NFJR
//http://feeds2.feedburner.com/OpenMania
package com.sun.syndication.samples;



import java.util.Calendar;
import java.util.Vector;



import com.franz.agbase.AllegroGraph;
import com.franz.agbase.AllegroGraphConnection;
import com.franz.agbase.AllegroGraphException;
import com.franz.agbase.TriplesIterator;
import com.sun.syndication.feed.synd.SyndCategoryImpl;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;

/**
 * 
 * 
 * @author 
 *
 */

public class FeedReader extends Thread{	
	private ManageFeeds feeds;
	private Vector<String> listFeeds=null;


	FeedReader(){

		this.run();
	}
	public void run() {
		feeds = new ManageFeeds();		
		feeds.readFeedsFile();
		listFeeds=feeds.getListFeeds();
		OntologyManager oManager = new OntologyManager();
		Calendar calendar = null; 
		AllegroGraphConnection ags = oManager.openConnection();
		AllegroGraph ts=oManager.openRDFStore(ags);  
		//Carregar a stopWordList
		oManager.loadStopWordList();

		while (true){			   
			//oManager.printTriples(ts);
			try {		
				for (int j=0; j<listFeeds.size(); j++){
					System.out.println("FEED: "+ j);
					SyndFeed feed = feeds.getFeed(feeds.getListFeeds().get(j)); 
					for (int i=0; i<feed.getEntries().size(); i++){					
						ts=oManager.insertFeed(ts,((SyndEntryImpl) (feed.getEntries().get(i))).getTitle(), ((SyndEntryImpl) (feed.getEntries().get(i))).getDescription().getValue(), ((SyndEntryImpl) (feed.getEntries().get(i))).getLink(), calendar.getInstance().get(Calendar.YEAR) +"-" + (calendar.getInstance().get(Calendar.MONTH)+1) + "-" + calendar.getInstance().get(Calendar.DAY_OF_MONTH),((SyndEntryImpl) feed.getEntries().get(i)).getCategories());																	
					}
				}
			}
			catch (Exception ex) {
				ex.printStackTrace();
				System.out.println("ERROR: "+ex.getMessage());
			}
			
			try {
				oManager.testReasoner(ts);
			} catch (AllegroGraphException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		//	oManager.printTriples(ts);

			try {				
				Thread.sleep(1800000);			    		    
			} catch (InterruptedException e) {
				System.out.println("Erro na thread");
			}

		}
	}


	public static void main (String args[]){

		Thread leitura = new Thread ( new FeedReader());	

	}
}